package com.bupt.liutong.admin.shop.bo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;

import com.bupt.liutong.admin.core.base.BaseBo;
import com.bupt.liutong.admin.core.base.FileUploader;
import com.bupt.liutong.admin.core.dao.ConfigDao;
import com.bupt.liutong.admin.core.dto.Config;
import com.bupt.liutong.admin.shop.dao.AttrDao;
import com.bupt.liutong.admin.shop.dao.AttrValDao;
import com.bupt.liutong.admin.shop.dao.ProdDao;
import com.bupt.liutong.admin.shop.dao.ProdPhotoDao;
import com.bupt.liutong.admin.shop.dao.ProdTypeDao;
import com.bupt.liutong.admin.shop.dto.Attr;
import com.bupt.liutong.admin.shop.dto.AttrVal;
import com.bupt.liutong.admin.shop.dto.Prod;
import com.bupt.liutong.admin.shop.dto.ProdPhoto;
import com.bupt.liutong.admin.shop.dto.ProdType;
import com.bupt.liutong.admin.shop.form.AttrForm;
import com.bupt.liutong.admin.shop.form.ProdForm;
import com.bupt.liutong.admin.shop.form.ProdPhotoForm;
import com.bupt.liutong.admin.shop.form.ProdTypeForm;
import com.bupt.liutong.util.BeanUtils;
import com.bupt.liutong.util.StringUtils;

public class CourseBo extends BaseBo {

	public static final int COURSE_ID = 2;
	
	private ProdDao prodDao;
	private ProdPhotoDao prodPhotoDao;
	private ProdTypeDao prodTypeDao;
	private AttrDao attrDao;
	private AttrValDao attrValDao;
	private ConfigDao configDao;
	private FileUploader fileUploader;
	
	public List<ProdForm> getCoursesByPage(ProdForm prodForm){
		Prod prod = new Prod();
		prodForm.setTopTypeId(COURSE_ID);	// 商品类型为课程
		sm.pagingWithNoCriteria(prodForm, prod, "shop_prod.getProdsCount");
		List<Prod> prods = prodDao.getProdsByPage(prod);
		return BeanUtils.dtoToForm(prods, ProdForm.class);
	}
	
	public List<ProdForm> getHotCoursesByPage(ProdForm prodForm){
		Prod prod = new Prod();
		prodForm.setTopTypeId(COURSE_ID);	// 商品类型为课程
		prodForm.setHot(1);					// 状态为接受报名
		sm.pagingWithNoCriteria(prodForm, prod, "shop_prod.getHotProdsCount");
		List<Prod> prods = prodDao.getHotProds(prod);
		return BeanUtils.dtoToForm(prods, ProdForm.class);
	}
	
	public List<ProdForm> getCoursesByTypeIdByPage(ProdForm prodForm){
		Prod prod = new Prod();
		sm.pagingWithNoCriteria(prodForm, prod, "shop_prod.getProdsCountByTypeId");
		List<Prod> prods = prodDao.getProdsByTypeIdByPage(prod);
		return BeanUtils.dtoToForm(prods, ProdForm.class);
	}
	
	public ProdForm getCourseById(ProdForm prodForm){
		ProdForm resultForm = new ProdForm();
		Prod prod = prodDao.getProdById(prodForm.getId());
		BeanUtils.copyProperties(resultForm, prod);
		return resultForm;
	}
	
	public String getTypeNameByTypeId(ProdForm prodForm){
		return prodTypeDao.getProdTypeById(prodForm.getTypeId()).getName();
	}
	
	public List<ProdPhotoForm> getCoursePhotosById(ProdForm prodForm){
		List<ProdPhoto> prodPhotos = prodPhotoDao.getProdPhotosByProdId(prodForm.getId());
		return BeanUtils.dtoToForm(prodPhotos, ProdPhotoForm.class);
	}
	
	public List<ProdForm> getProfCoursesByPage(ProdForm prodForm){
		Prod prod = new Prod();
		sm.pagingWithNoCriteria(prodForm, prod, "shop_prod.getProfCoursesCount");
		List<Prod> prods = prodDao.getProfCoursesByPage(prod);
		return BeanUtils.dtoToForm(prods, ProdForm.class);
	}
	
	public List<ProdForm> getUnProfCoursesByPage(ProdForm prodForm){
		Prod prod = new Prod();
		sm.pagingWithNoCriteria(prodForm, prod, "shop_prod.getUnProfCoursesCount");
		List<Prod> prods = prodDao.getUnProfCoursesByPage(prod);
		return BeanUtils.dtoToForm(prods, ProdForm.class);
	}
	
	public String getIntro(){
		return configDao.getConfigByCode("shop_course_intro");
	}
	
	@Transactional
	public void insertCourse(ProdForm prodForm, HttpServletRequest request) {
		// 1. insert basic info
		Prod prod = new Prod();
		BeanUtils.copyProperties(prod, prodForm);
		Integer prodId = prodDao.insertProd(prod);
		// 2. insert arr
		Integer[] attrIds = prodForm.getAttrIds();
		String[] attrVals = prodForm.getAttrVals();
		for (int i = 0; i < attrIds.length; i++) {
			if(!StringUtils.isEmpty(attrVals[i]))
				attrValDao.insertAttrVal(new AttrVal(prodId, attrIds[i], attrVals[i]));
		}
		// 3. pic
		fileUploader.uploadImages(prodForm, prodPhotoDao, request);
	}
	
	@Transactional
	public void updateCourse(ProdForm prodForm, HttpServletRequest request) {
		// 1. update basic info
		Prod prod = new Prod();
		BeanUtils.copyProperties(prod, prodForm);
		prodDao.updateProd(prod);
		// 2. update or insert arr
		Integer[] attrIds = prodForm.getAttrIds();
		Integer[] attrValIds = prodForm.getAttrValIds();
		String[] attrVals = prodForm.getAttrVals();
		for (int i = 0; i < attrIds.length; i++) {
			if(attrValIds[i] != null)
				attrValDao.updateAttrVal(new AttrVal(attrValIds[i], attrVals[i]));
			else if (!StringUtils.isEmpty(attrVals[i]))
				attrValDao.insertAttrVal(new AttrVal(prod.getId(), attrIds[i], attrVals[i]));
		}
		// 3. pic
		fileUploader.updateImages(prodForm, prodPhotoDao, request);
		fileUploader.uploadImages(prodForm, prodPhotoDao, request);
	}
	
	public void updateIntro(HttpServletRequest request) {
		String shop_course_intro = request.getParameter("shop_course_intro");
		Config config = new Config("shop_course_intro", shop_course_intro);
		configDao.updateConfigByCode(config);
	}
	
	@Transactional
	public void enableCourses(String[] ids) {
		if (ids != null) {
			for (String id : ids) {
				Prod prod = new Prod();
				prod.setId(Integer.valueOf(id));
				prod.setEnabled(1);
				prodDao.updateProdState(prod);
			}
		}
	}
	
	@Transactional
	public void disableCourses(String[] ids) {
		if (ids != null) {
			for (String id : ids) {
				Prod prod = new Prod();
				prod.setId(Integer.valueOf(id));
				prod.setEnabled(0);
				prodDao.updateProdState(prod);
			}
		}
	}
	
	@Transactional
	public void updateCourseHot(String[] ids) {
		if (ids != null) {
			for (String id : ids) {
				Prod prod = new Prod();
				prod.setId(Integer.valueOf(id));
				prod.setHot(1);
				prodDao.updateProdHot(prod);
			}
		}
	}
	
	@Transactional
	public void updateCourseUnHot(String[] ids) {
		if (ids != null) {
			for (String id : ids) {
				Prod prod = new Prod();
				prod.setId(Integer.valueOf(id));
				prod.setHot(0);
				prodDao.updateProdHot(prod);
			}
		}
	}
	
	@Transactional
	public void deleteCourses(String[] ids) {
		if (ids != null) {
			for (String id : ids) {
				Integer prodId = Integer.valueOf(id);
				prodPhotoDao.deleteProdPhotoByProdId(prodId);
				attrValDao.deleteAttrValByProdId(prodId);
				prodDao.deleteProd(prodId);
			}
		}
	}
	
	public List<AttrForm> getAttrs(){
		List<Attr> attrs = attrDao.getAttrsByTypeId(COURSE_ID);
		return BeanUtils.dtoToForm(attrs, AttrForm.class);
	}

	public Map<Integer, AttrVal> getAttrVals(ProdForm prodForm) {
		List<AttrVal> attrVals = attrValDao.getAttrValsByProdId(prodForm
				.getId());
		Map<Integer, AttrVal> attrValMap = new HashMap<Integer, AttrVal>();
		for (AttrVal attrVal : attrVals)
			attrValMap.put(attrVal.getAttrId(), attrVal);
		return attrValMap;
	}
	
	public List<ProdTypeForm> getTypes(){
		List<ProdType> prodTypes = prodTypeDao.getProdTypesByTopParentId(COURSE_ID);
		return BeanUtils.dtoToForm(prodTypes, ProdTypeForm.class);
	}

	public void setProdDao(ProdDao prodDao) {
		this.prodDao = prodDao;
	}

	public void setAttrDao(AttrDao attrDao) {
		this.attrDao = attrDao;
	}

	public void setProdTypeDao(ProdTypeDao prodTypeDao) {
		this.prodTypeDao = prodTypeDao;
	}

	public void setAttrValDao(AttrValDao attrValDao) {
		this.attrValDao = attrValDao;
	}

	public void setFileUploader(FileUploader fileUploader) {
		this.fileUploader = fileUploader;
	}

	public void setProdPhotoDao(ProdPhotoDao prodPhotoDao) {
		this.prodPhotoDao = prodPhotoDao;
	}

	public static int getCOURSE_ID() {
		return COURSE_ID;
	}

	public void setConfigDao(ConfigDao configDao) {
		this.configDao = configDao;
	}
}
